<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./OL_SDK/include-openlayers-local.js"></script>
  <script src="./libs/gaode.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0
    }

    #map {
      width: 100vw;
      height: 100vh;
    }

    button {
      width: 200px;
      height: 50px;
      position: fixed;
      top: 20px;
      z-index: 100;
    }

    .draw button:first-child {
      left: 50px;
    }

    .draw button:last-child {
      left: 300px;
    }
  </style>
</head>

<body>
  <div class="draw">
    <button id="btn" onclick="activeDraw()">激活画笔</button>
    <button onclick="removeDraw()">移除画笔</button>
  </div>

  <div id="map">
  </div>
  <script>
    var btn = document.getElementById("btn");
    var map = new ol.Map({
      target: "map",
      layers: [gaode],
      view: new ol.View({
        projection: 'EPSG:4326',
        center: [114.30, 30.50],
        zoom: 4
      })
    })
    /* 1、设置画布  layer-source */
    let source = new ol.source.Vector({

    })
    let layer = new ol.layer.Vector({
      source
    })
    map.addLayer(layer)
    let draw;
    function activeDraw() {
      btn.disabled = true;
      btn.style.cursor = "no-drop"
      draw = new ol.interaction.Draw({
        type: "Point",
        source
      })
      map.addInteraction(draw);
      // 监听画笔 -- 画笔绘制结束   -- 画笔绘制结束后，移除画笔   -- 恢复按钮状态   -- 恢复鼠标状态
      // draw.on("drawend", evt => {
      //   console.log(evt);
      //   // map.removeInteraction(draw)
      //   btn.disabled = false;
      //   btn.style.cursor = "default"
      // })
    }

    function removeDraw() {
      // 移除画笔方法一
      // map.removeInteraction(draw);
      //移除画笔方法二
      map.getInteractions().pop()
      btn.disabled = false;
      btn.style.cursor = "default"
    }
  </script>
</body>

</html>